/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "connector_dev.h"
#include "io.h"
#include "drv_gpio.h"
#include "k_vo_comm.h"
#include "k_autoconf_comm.h"
#include "k_connector_comm.h"

static void lcd_init(void)
{
    const k_u8 init_sequence[] = {
        // cmd type, delay, data length, data0 ... dataN
        0x39, 0x00, 0x06, 0xF0, 0x55, 0xaa, 0x52, 0x08, 0x00,
        0x39, 0x00, 0x03, 0xF6, 0x5a, 0x87,
        0x39, 0x00, 0x02, 0xC1, 0x3F,
        0x39, 0x00, 0x02, 0xCD, 0x25,
        0x39, 0x00, 0x02, 0xC9, 0x10,
        0x39, 0x00, 0x02, 0xF8, 0x8A,
        0x39, 0x00, 0x02, 0xAC, 0x45,
        0x39, 0x00, 0x02, 0xA7, 0x47,
        0x39, 0x00, 0x02, 0xA0, 0xCC,
        0x39, 0x00, 0x05, 0x86, 0x99, 0xA3, 0xA3, 0x41,
        0x39, 0x00, 0x05, 0xFA, 0x08, 0x08, 0x00, 0x04,
        0x39, 0x00, 0x02, 0xA3, 0x6E,
        0x39, 0x00, 0x04, 0xFD, 0x3c, 0x3C, 0x00,
        0x39, 0x00, 0x02, 0x9A, 0x6c,
        0x39, 0x00, 0x02, 0x9B, 0x6c,
        0x39, 0x00, 0x03, 0x82, 0x42, 0x42,
        0x39, 0x00, 0x02, 0xB1, 0x10,
        0x39, 0x00, 0x03, 0x7A, 0x0F, 0x13,
        0x39, 0x00, 0x03, 0x7B, 0x0F, 0x13,
        0x39, 0x00, 0x08, 0x69, 0x14, 0x22, 0x14, 0x22, 0x44, 0x22, 0x08,
        0x39, 0x00, 0x02, 0x6B, 0x07,
        0x39, 0x00, 0x21, 0x6D, 0x07, 0x1e, 0x01, 0x00, 0x1F, 0x09, 0x0B, 0x0D, 0x0F, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x10, 0x0E, 0x0C, 0x0A, 0x1F, 0x00, 0x02, 0x1E, 0x08,
        0x39, 0x00, 0x09, 0x60, 0x18, 0x07, 0x7a, 0x7a, 0x18, 0x06, 0x7a, 0x7a,
        0x39, 0x00, 0x09, 0x63, 0x13, 0x1f, 0x7a, 0x7a, 0x13, 0x20, 0x7a, 0x7a,
        0x39, 0x00, 0x11, 0x64, 0x18, 0x03, 0x03, 0x25, 0x03, 0x03, 0x18, 0x02, 0x03, 0x26, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a,
        0x39, 0x00, 0x11, 0x65, 0x18, 0x01, 0x03, 0x27, 0x03, 0x03, 0x18, 0x00, 0x03, 0x28, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a,
        0x39, 0x00, 0x11, 0x66, 0x10, 0x01, 0x03, 0x29, 0x03, 0x03, 0x10, 0x02, 0x03, 0x2a, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a,
        0x39, 0x00, 0x11, 0x67, 0x10, 0x03, 0x03, 0x2b, 0x03, 0x03, 0x10, 0x04, 0x03, 0x2c, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a,
        0x39, 0x00, 0x35, 0xD1, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x24, 0x00, 0x30, 0x00, 0x40, 0x00, 0x59, 0x00, 0x80, 0x00, 0x9d, 0x00, 0xcf, 0x00, 0xfc, 0x01, 0x50, 0x01, 0x9e, 0x01, 0xA0, 0x01, 0xE8, 0x02, 0x46, 0x02, 0x81, 0x02, 0xcc, 0x03, 0x02, 0x03, 0x60, 0x03, 0xb0, 0x03, 0xc1, 0x03, 0xcd, 0x03, 0xd8, 0x03, 0xe0, 0x03, 0xFF,
        0x39, 0x00, 0x35, 0xD2, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x24, 0x00, 0x30, 0x00, 0x40, 0x00, 0x59, 0x00, 0x80, 0x00, 0x9d, 0x00, 0xcf, 0x00, 0xfc, 0x01, 0x50, 0x01, 0x9e, 0x01, 0xA0, 0x01, 0xE8, 0x02, 0x46, 0x02, 0x81, 0x02, 0xcc, 0x03, 0x02, 0x03, 0x60, 0x03, 0xb0, 0x03, 0xc1, 0x03, 0xcd, 0x03, 0xd8, 0x03, 0xe0, 0x03, 0xFF,
        0x39, 0x00, 0x35, 0xD3, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x24, 0x00, 0x30, 0x00, 0x40, 0x00, 0x59, 0x00, 0x80, 0x00, 0x9d, 0x00, 0xcf, 0x00, 0xfc, 0x01, 0x50, 0x01, 0x9e, 0x01, 0xA0, 0x01, 0xE8, 0x02, 0x46, 0x02, 0x81, 0x02, 0xcc, 0x03, 0x02, 0x03, 0x60, 0x03, 0xb0, 0x03, 0xc1, 0x03, 0xcd, 0x03, 0xd8, 0x03, 0xe0, 0x03, 0xFF,
        0x39, 0x00, 0x35, 0xD4, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x24, 0x00, 0x30, 0x00, 0x40, 0x00, 0x59, 0x00, 0x80, 0x00, 0x9d, 0x00, 0xcf, 0x00, 0xfc, 0x01, 0x50, 0x01, 0x9e, 0x01, 0xA0, 0x01, 0xE8, 0x02, 0x46, 0x02, 0x81, 0x02, 0xcc, 0x03, 0x02, 0x03, 0x60, 0x03, 0xb0, 0x03, 0xc1, 0x03, 0xcd, 0x03, 0xd8, 0x03, 0xe0, 0x03, 0xFF,
        0x39, 0x00, 0x35, 0xD5, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x24, 0x00, 0x30, 0x00, 0x40, 0x00, 0x59, 0x00, 0x80, 0x00, 0x9d, 0x00, 0xcf, 0x00, 0xfc, 0x01, 0x50, 0x01, 0x9e, 0x01, 0xA0, 0x01, 0xE8, 0x02, 0x46, 0x02, 0x81, 0x02, 0xcc, 0x03, 0x02, 0x03, 0x60, 0x03, 0xb0, 0x03, 0xc1, 0x03, 0xcd, 0x03, 0xd8, 0x03, 0xe0, 0x03, 0xFF,
        0x39, 0x00, 0x35, 0xD6, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x00, 0x24, 0x00, 0x30, 0x00, 0x40, 0x00, 0x59, 0x00, 0x80, 0x00, 0x9d, 0x00, 0xcf, 0x00, 0xfc, 0x01, 0x50, 0x01, 0x9e, 0x01, 0xA0, 0x01, 0xE8, 0x02, 0x46, 0x02, 0x81, 0x02, 0xcc, 0x03, 0x02, 0x03, 0x60, 0x03, 0xb0, 0x03, 0xc1, 0x03, 0xcd, 0x03, 0xd8, 0x03, 0xe0, 0x03, 0xFF,
        0x39, 0x00, 0x02, 0x11, 0x00,
        0x39, 0xff, 0x02, 0x29, 0x00,
    };

    connector_send_cmd(init_sequence, sizeof(init_sequence), K_FALSE);
}

static void gc9503_power_reset(k_s32 on)
{
    k_u8 rst_gpio;

    rst_gpio = CONFIG_MPP_DSI_LCD_RESET_PIN;

    // rt_kprintf("rst_gpio is %d \n",rst_gpio);

    kd_pin_mode(rst_gpio, GPIO_DM_OUTPUT);

    if (on)
        kd_pin_write(rst_gpio, GPIO_PV_HIGH); // GPIO_PV_LOW  GPIO_PV_HIGH
    else
        kd_pin_write(rst_gpio, GPIO_PV_LOW); // GPIO_PV_LOW  GPIO_PV_HIGH

}

static void lcd_deinit(void)
{
    gc9503_power_reset(1);
    rt_thread_mdelay(50);
    gc9503_power_reset(0);
    rt_thread_mdelay(50);
    gc9503_power_reset(1);
}

static void gc9503_set_backlight(k_s32 on)
{
    k_u8 backlight_gpio;

    if(CONFIG_MPP_DSI_LCD_BACKLIGHT_PIN == 255) // unused
        return;

    backlight_gpio = CONFIG_MPP_DSI_LCD_BACKLIGHT_PIN;

    // rt_kprintf("backlight_gpio is %d \n",backlight_gpio);

    kd_pin_mode(backlight_gpio, GPIO_DM_OUTPUT);
    if (on)
        kd_pin_write(backlight_gpio, GPIO_PV_HIGH);
    else
        kd_pin_write(backlight_gpio, GPIO_PV_LOW);

}


static k_s32 gc9503_power_on(void* ctx, k_s32 on)
{
    k_s32 ret = 0;
    struct connector_driver_dev* dev = ctx;
    // rst vo;
    k230_display_rst();

    if (on) {

        // rst gc9503
        gc9503_power_reset(1);
        rt_thread_mdelay(50);
        gc9503_power_reset(0);
        rt_thread_mdelay(50);
        gc9503_power_reset(1);

        rt_thread_mdelay(120);
        //enable backlight
        gc9503_set_backlight(1);
    } else {
        gc9503_set_backlight(0);
        lcd_deinit();
    }

    return ret;
}


static k_s32 gc9503_set_phy_freq(k_connectori_phy_attr *phy_attr)
{
    k_vo_mipi_phy_attr mipi_phy_attr;

    memset(&mipi_phy_attr, 0, sizeof(k_vo_mipi_phy_attr));

    mipi_phy_attr.m = phy_attr->m;
    mipi_phy_attr.n = phy_attr->n;
    mipi_phy_attr.hs_freq = phy_attr->hs_freq;
    mipi_phy_attr.voc = phy_attr->voc;
    mipi_phy_attr.phy_lan_num = K_DSI_2LAN;
    connector_set_phy_freq(&mipi_phy_attr);
    // rt_kprintf("config phy success phy_attr->m is %d  phy_attr->n is %d phy_attr->hs_freq is %d phy_attr->voc is %d \n", phy_attr->m,  phy_attr->n, phy_attr->hs_freq, phy_attr->voc);
    return 0;
}


static k_s32 gc9503_dsi_resolution_init(k_connector_info *info)
{
    k_vo_dsi_attr attr;
    k_vo_display_resolution resolution;

    memset(&attr, 0, sizeof(k_vo_dsi_attr));
    attr.lan_num = info->lan_num;
    attr.cmd_mode = info->cmd_mode;
    attr.lp_div = 8;
    attr.work_mode = info->work_mode;
    memcpy(&resolution, &info->resolution, sizeof(k_vo_display_resolution));
    memcpy(&attr.resolution, &resolution, sizeof(k_vo_display_resolution));
    connector_set_dsi_attr(&attr);

    lcd_init();

    connector_set_dsi_enable(1);

    if(info->dsi_test_mode == 1)
        connector_set_dsi_test_mode();

    return 0;
}


static k_s32 gc9503_vo_resolution_init(k_vo_display_resolution *resolution, k_u32 bg_color, k_u32 intr_line)
{
    k_vo_display_resolution vo_resolution;
    k_vo_pub_attr attr;

    memset(&attr, 0, sizeof(k_vo_pub_attr));
    attr.bg_color = bg_color;
    attr.intf_sync = K_VO_OUT_1080P30;
    attr.intf_type = K_VO_INTF_MIPI;
    attr.sync_info = resolution;

    connector_set_vo_init();
    connector_set_vtth_intr(1, intr_line);
    connector_set_vo_param(&attr);
    connector_set_vo_enable();

    return 0;
}


static k_s32 gc9503_init(void *ctx, k_connector_info *info)
{
    k_s32 ret = 0;
    struct connector_driver_dev* dev = ctx;

    if(info->pixclk_div != 0)
        connector_set_pixclk(info->pixclk_div);

    ret |= gc9503_set_phy_freq(&info->phy_attr);
    ret |= gc9503_dsi_resolution_init(info);
    ret |= gc9503_vo_resolution_init(&info->resolution, info->bg_color, info->intr_line);

    return ret;
}

static k_s32 gc9503_get_chip_id(void* ctx, k_u32* chip_id)
{
    k_s32 ret = 0;

    return ret;
}


static k_s32 gc9503_conn_check(void* ctx, k_s32* conn)
{
    k_s32 ret = 0;

    *conn = 1;

    return ret;
}

struct connector_driver_dev gc9503_connector_drv = {
    .connector_name = "gc9503",
    .connector_func = {
        .connector_power = gc9503_power_on,
        .connector_init = gc9503_init,
        .connector_get_chip_id = gc9503_get_chip_id,
        .connector_conn_check = gc9503_conn_check,
    },
};
